package com.network.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.FragmentManager;

import com.network.BuildConfig;
import com.network.retrofit_rx.Api.CommandImpl;
import com.network.update.IGetUpdateInfo;
import com.network.update.SoftUpdate;

/**
 * @author QiQi
 * @ClassName: UpdateUtil
 * @date 2018/10/19
 * @description
 */
public class UpdateUtil {
    /**
     * 软件升级检查
     *
     * @param context
     * @param fragmentManager
     * @param isTip           是否需要吐司提示
     */
    public static void softWareUpdate(final Context context, final FragmentManager fragmentManager, final boolean isTip, IGetUpdateInfo updateInfo) {
        if(BuildConfig.DEBUG){
            return;
        }
        CommandImpl command = new CommandImpl.Builder()
                .setUrl("upgrade/app/query")
                .addParams("version", getMyVersion(context))
                .create();


        SoftUpdate.checkUpdate(context, command, fragmentManager, updateInfo, isTip);
    }

    public static String getMyVersion(Context ctx) {
        try {
            PackageManager packageManager = ctx.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(ctx.getPackageName(), 0);
            return packInfo.versionName;
        } catch (Exception e) {
        }
        return "";
    }
}
